package shuashuami.hb.com.fragment.agent;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.DistributeCenterAdapter;
import shuashuami.hb.com.entity.agent.OrderBean;
import shuashuami.hb.com.entity.agent.PagerBean;
import shuashuami.hb.com.fragment.AbFragment;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.http.LoginHttp;

/**
 * created by zhangqi at 2017/7/14
 */
public class DistributeCompleteFragm2 extends AbFragment{

    private ListView lv_dist_center_items;
    private int disType=0;  // 1表示完成订单

    private int currentPage=1;

    private DistributeCenterAdapter adapter;

    private List<OrderBean> mResources;



    public DistributeCompleteFragm2(){

    }
    @Override
    public void initView(View v) {
//        int type= getArguments().getInt("type");
//
//        disType=type;
        lv_dist_center_items= (ListView) findViewById(R.id.lv_dist_center_items);

    }

    @Override
    public void initData() {
        mResources=new ArrayList<>();

        adapter=new DistributeCenterAdapter(getActivity(),mResources);

        lv_dist_center_items.setAdapter(adapter);


    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        getDataFromSever();
    }

    @Override
    public void initListener() {

    }

    @Override
    public int bindView() {
        return R.layout.frag_distribute2;
    }

    public void getDataFromSever(){
//        HttpOnNextListener<String> listener=new HttpOnNextListener<String>() {
//            @Override
//            public void onNext(final String o) {
//                getAbActivity().runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        resolveByJson(o);
//                    }
//                });
//
//
//
//            }
//        };
//
//        HttpAgentMethods.getAgentOrders(listener,0,currentPage);
    }

    public void resolveByJson(String o){
        PagerBean<OrderBean> pagerBean=new PagerBean<>();
        try{

            JSONObject object=new JSONObject(o);
            int status= object.getInt("status");
            if (status == 203) {
                LoginHttp loginHttp = new LoginHttp(getActivity());
                loginHttp.login();
                return;
            }
            if(status==200){
                JSONObject content=object.getJSONObject("content");
                pagerBean.setCount(content.getInt("count"));
                pagerBean.setMaxpage(content.getInt("maxpage"));
                pagerBean.setCurrentpage(content.getInt("currentpage"));
                JSONArray list=content.getJSONArray("list");
                List<OrderBean> items=new ArrayList<>();
                for(int i=0;i<list.length();i++){
                    JSONObject item=list.getJSONObject(i);
                    OrderBean bean=new OrderBean();
                    bean.resolveByJson(item);
                    items.add(bean);
                }

                pagerBean.setList(items);
                mResources.addAll(pagerBean.getList());
                mHandler.sendEmptyMessage(0x1);

            }
            else{
                String message=object.getString("message");
                Toast.makeText(getAbActivity(),message,Toast.LENGTH_LONG).show();
            }

        }catch (Exception e){

        }



    }


    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int what=msg.what;
            if(what==0x1){
                adapter.notifyDataSetChanged();
            }
        }
    };
}
